<?php
/*
 * Created on 2009/08/03
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 
class VariableResponse
{
	public static function execute($botObj, $tweet, $action_value)
	{
		$sql = "
		SELECT response.value, response.id
		FROM response
		INNER JOIN action_response ON action_response.response_id = response.id
		WHERE action_response.action_id = $action_value
		AND response.value <> '" . $botObj->db->real_escape_string($botObj->lastStatusUpdate) . "'
		AND used = 0
		ORDER BY RAND()
		";
		
		$response_result = $botObj->db->query($sql);
		
		if($botObj->db->error)
		{
			$botObj->logEvent("DBエラー： " . $botObj->db->error);
		}
		
		//　最後のランダム返事なので全てをリセット
		if($response_result->num_rows == 1)
		{
			$sql = "
			UPDATE response
			INNER JOIN action_response ON action_response.response_id = response.id
			SET used = 0
			WHERE action_response.action_id = $action_value
			";
			
			$usedResetResult = $botObj->db->query($sql);
		}
		
		$response = $response_result->fetch_assoc();
		$response_result->close();
		
		$botObj->pluginResponse['tweet'] = $response['value'];
		$sql = "
		UPDATE response
		SET used = 1
		WHERE id = {$response['id']}
		";
		
		$responseUsedUpdate = $botObj->db->query($sql);
	}
}
?>
